from Tools import mathtools as mt
class cell:
    def __init__(self):
        self.pre = []
        self.lat = []
        self.lon = []
        self.time = []
        self.real = []
        self.prn = []
        self.cynum = []
        self.channel=[]

def Judgment_and_Splitting(raw_data: cell,time_dis:float,distance:float,ws_limite:float):
    """
    time_dis：为时间间隔，单位秒
    distance：为距离间隔，单位为km
    ws_limit：为结果限定值，根据情况设定，对于风速建议设置为5

    """
    
    if len(raw_data.lat) > 0:
        res_cell = cell()
        mid_cel = cell()
        for i in range(1,len(raw_data.lat)):
            if raw_data.time[i]-raw_data.time[i-1]<time_dis and mt.distance((raw_data.lon[i], raw_data.lat[i]), (
                        raw_data.lon[i - 1], raw_data.lat[i - 1]))<distance and abs(
                    raw_data.pre[i] - raw_data.pre[i - 1]) <ws_limite:
                mid_cel.pre.append(raw_data.pre[i])
                mid_cel.lat.append(raw_data.lat[i])
                mid_cel.lon.append(raw_data.lon[i])
                mid_cel.time.append(raw_data.time[i])
                mid_cel.real.append(raw_data.real[i])
                mid_cel.prn.append(raw_data.prn[i])
                mid_cel.cynum.append(raw_data.cynum[i])
                mid_cel.channel.append(raw_data.channel[i])
            else:
                if len(mid_cel.lat)>5:
                    res_cell.pre.extend(mid_cel.pre)
                    res_cell.time.extend(mid_cel.time)
                    res_cell.lat.extend(mid_cel.lat)
                    res_cell.lon.extend(mid_cel.lon)
                    res_cell.real.extend(mid_cel.real)
                    res_cell.prn.extend(mid_cel.prn)
                    res_cell.cynum.extend(mid_cel.cynum)
                    res_cell.channel.extend(mid_cel.channel)
                    mid_cel = cell()
                else:
                    mid_cel = cell()
        return res_cell
    else:
        return cell()


def Trajectory_discontinuity_method(obs_para:dict,pre_data:list or np.array,time_dis:float=0.51,distance:float=3.0,ws_limite:float=5):
    content = {str(gps + 1): {str(cy + 1): {str(ch): cell() for ch in range(0, 4)} for cy in range(0, 8)} for gps in
               range(0, 32)}
    content2 = {str(gps + 1): {str(cy + 1): {str(ch): cell() for ch in range(0, 4)} for cy in range(0, 8)} for gps in
                range(0, 32)}
    for i in range(0, len(obs_para["prn"])):
        content[str(int(obs_para["prn"][i]))][str(int(obs_para["cynum"][i]))][
            str(int(obs_para["channel"][i]))].pre.append(pre_data[i])
        content[str(int(obs_para["prn"][i]))][str(int(obs_para["cynum"][i]))][
            str(int(obs_para["channel"][i]))].real.append(obs_para["ews"][i])
        content[str(int(obs_para["prn"][i]))][str(int(obs_para["cynum"][i]))][
            str(int(obs_para["channel"][i]))].lat.append(
            obs_para["lat"][i])
        content[str(int(obs_para["prn"][i]))][str(int(obs_para["cynum"][i]))][
            str(int(obs_para["channel"][i]))].lon.append(
            obs_para["lon"][i])
        content[str(int(obs_para["prn"][i]))][str(int(obs_para["cynum"][i]))][
            str(int(obs_para["channel"][i]))].time.append(
            obs_para["time"][i])
        content[str(int(obs_para["prn"][i]))][str(int(obs_para["cynum"][i]))][
            str(int(obs_para["channel"][i]))].prn.append(
            obs_para["prn"][i])
        content[str(int(obs_para["prn"][i]))][str(int(obs_para["cynum"][i]))][
            str(int(obs_para["channel"][i]))].cynum.append(
            obs_para["cynum"][i])
        content[str(int(obs_para["prn"][i]))][str(int(obs_para["cynum"][i]))][
            str(int(obs_para["channel"][i]))].channel.append(
            obs_para["channel"][i])

        
        res = {"time":[],"lat":[],"lon":[],"prn":[],"channel":[],"pre":[],"real":[],"cynum":[]}
        
        
    for prn in content2.keys():
        for cygnss in content2[prn].keys():
            for channel in content2[prn][cygnss]:
                print(prn,cygnss,channel)
                mid = Judgment_and_Splitting(content[prn][cygnss][channel],time_dis=time_dis,distance=distance,ws_limite=ws_limite)
                res["time"].extend(mid.time)
                res["lat"].extend(mid.lat)
                res["lon"].extend(mid.lon)
                res["prn"].extend(mid.prn)
                res["channel"].extend(mid.channel)
                res["cynum"].extend(mid.cynum)
                res["real"].extend(mid.real)
                res["pre"].extend(mid.pre)
    return res
    
    

    
    
    



